package com.jjwxc.jwjskandriod.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.base.FFApplication;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.model.TagListResopnse;

import java.util.ArrayList;
import java.util.List;

public class TabScreenPop {

    public void tabScreenPopWindow(View v, Activity context, List<String> mIds, PopTabScreenListener popTabScreenListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_screen_pop, null, false);

        TagGroupView tgv_one = view.findViewById(R.id.tgv_one);

        TextView tv_ok = view.findViewById(R.id.tv_ok);
        TextView tv_quxiao = view.findViewById(R.id.tv_quxiao);
        LinearLayout ll_quxiao = view.findViewById(R.id.ll_quxiao);
        LinearLayout ll_one = view.findViewById(R.id.ll_one);

        int height = context.getResources().getDisplayMetrics().heightPixels;// 屏幕的高
        //1.构造一个PopupWindow，参数依次是加载的 View，宽高 //设置popupWindow的高为屏幕的高+顶部状态栏的高
        PopupWindow popWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT
                , height + getStatusBarHeight(context), true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.ffProg_anim);
        popWindow.setClippingEnabled(false);

        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(() -> {
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

        ll_quxiao.setOnClickListener(v1 -> popWindow.dismiss());
        ll_one.setOnClickListener(v1 -> popWindow.dismiss());

        //设置 popupWindow 里的按钮的事件
        List<String> list = new ArrayList<>();
        List<String> listId = new ArrayList<>();
        ((FFActivity) context).post(Url.listTag, null, TagListResopnse.class, new FFNetWorkCallBack<TagListResopnse>() {
            @Override
            public void onSuccess(TagListResopnse response) {
                if (response.getCode() == 200) {
                    for (TagListResopnse.DataBean bean : response.getData()) {
                        list.add(bean.getName());
                        listId.add(bean.getTagId());
                    }
                    tgv_one.requestShowMeasure(true);
                    tgv_one.setCheckBoxTags(list, listId, R.layout.tag_screen_chackbox);
                    List<Integer> sList = new ArrayList<>();
                    for (int i = 0; i < mIds.size(); i++) {
                        for (int s = 0; s < listId.size(); s++) {
                            if (mIds.get(i).equals(listId.get(s))) {
                                sList.add(s);
                            }
                        }
                    }
                    tgv_one.selecteCheckBoxStateDefault(sList);
                }
            }

            @Override
            public boolean onFail(TagListResopnse response) {
                return false;
            }
        });

        tv_quxiao.setOnClickListener(v12 -> tgv_one.clearCheckBoxState());

        tv_ok.setOnClickListener(v13 -> {
            if (tgv_one.checkBoxSelect().size() > 0) {
                popTabScreenListener.onValueClick(tgv_one.checkBoxSelect());
            } else {
                popTabScreenListener.onValueClick(null);
//                FFApplication.showToast("至少选一个");
            }
            popWindow.dismiss();
        });
    }

    public interface PopTabScreenListener {
        void onValueClick(List<String> value);
    }

    /**
     * 获取状态栏高度
     *
     * @param context context
     * @return 状态栏高度
     */
    public int getStatusBarHeight(Context context) {
        // 获得状态栏高度
        @SuppressLint("InternalInsetResource")
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }


}
